package com.click369.crm.controller;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.click369.crm.common.ResultVo;
import com.click369.crm.pojo.CRole;
import com.click369.crm.service.FunctionService;
import com.click369.crm.service.RoleService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("role")
public class RoleController implements BaseController<CRole>{
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private FunctionService functionService;
	
	//添加
	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultVo save(CRole r) {
		r.setId(null);
		roleService.save(r);
		return new ResultVo(200,"增加成功");
	}
	
	//根据id删除
	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultVo delete(Integer id) {
		roleService.delete(id);
		return new ResultVo(200,"删除成功");
	}
	
	//修改
	@RequestMapping("update")
	@ResponseBody
	@Override
	public ResultVo update(CRole r) {
		Integer id = r.getId();
		if(id == null){
			return new ResultVo(200,"没有id");
		}
		roleService.update(r);
		return new ResultVo(200,"修改成功");
	}
	
	//查询所有分页
	@RequestMapping("list")
	@ResponseBody
	@Override
	public ResultVo select(Integer page, Integer rows, CRole r) {
		PageInfo info = roleService.select(page, rows, r);
		return new ResultVo(info.getTotal(), info.getList());
	}
	
	//根据id查询
	@RequestMapping("getById")
	@ResponseBody
	@Override
	public ResultVo getById(Integer id) {
		CRole byid = roleService.getById(id);
		return new ResultVo(byid);
	}
	
	//根据id查询
	@RequiresRoles({"sysadmin"})
	@RequestMapping("updateFuns")
	@ResponseBody
	public ResultVo updateFuns(Integer rid,Integer[] fids) {
		functionService.editRoleFunction(rid, fids);
		return new ResultVo();
	}
}
